import 'dart:core';

import '../dao/ImContentSearchDao.dart';
import '../dao/ImGroupDao.dart';
import '../dao/ImGroupUserDao.dart';
import '../dao/ImSessionDao.dart';
import '../dao/ImUserDao.dart';
import '../pojo/UiPojo.dart';
import 'BaseService.dart';

class ContentSearchService extends BaseService {

  Future<SearchResultSet> searchGroupAndUser(String userId, String searchContent) async {
    List<SearchResult> searchUserResult = await searchUsers(searchContent);
    List<SearchResult> searchGroupResult = await searchGroups(userId, searchContent);

    // SearchResultGroup userResultGroup = SearchResultGroup(searchUserResult.length, searchUserResult);
    // SearchResultGroup groupResultGroup = SearchResultGroup(searchGroupResult.length, searchGroupResult);
    return SearchResultSet(userSearchResults: searchUserResult, groupSearchResults: searchGroupResult);
  }

  // im 查询
  Future<SearchResultSet> search(String userId, String searchContent) async {
    List<SearchResult> searchUserResult = await searchUsers(searchContent);
    List<SearchResult> searchGroupResult = await searchGroups(userId, searchContent);
    List<SearchResult> searchMessageResult = await searchMessages(userId, searchContent);

    // SearchResultGroup userResultGroup = SearchResultGroup(searchUserResult.length, searchUserResult);
    // SearchResultGroup groupResultGroup = SearchResultGroup(searchGroupResult.length, searchGroupResult);
    // SearchResultGroup messageResultGroup = SearchResultGroup(searchMessageResult.length, searchMessageResult);
    return SearchResultSet(userSearchResults: searchUserResult,
        groupSearchResults: searchGroupResult,
        messageSearchResults: searchMessageResult);
}

  /// 查询用户
  Future<List<SearchResult>> searchUsers(String searchContent) async {
    final contentSearchDao = ImContentSearchDao();
    return await contentSearchDao.searchUsers(searchContent);
  }

  /// 查询群用户和群
  Future<List<SearchResult>> searchGroups(String userId, String searchContent) async {
    final contentSearchDao = ImContentSearchDao();
    List<SearchResult> results =  await contentSearchDao.searchGroups(userId, searchContent);
    for (var searchResult in results) {
      await contentSearchDao.loadGroupInfo(userId, searchResult);
    }
    return results;
  }

  /// 查询消息
  Future<List<SearchResult>> searchMessages(String userId, String searchContent) async {
    final contentSearchDao = ImContentSearchDao();
    List<SearchResult> result = await contentSearchDao.searchMessages(userId, searchContent);
    if (result.isNotEmpty) {
      var sessionDao = ImSessionDao();
      var groupDao = ImGroupDao();
      var userDao = ImUserDao();
      for (var searchResult in result) {
        var session = await sessionDao.getById(searchResult.sessionId!);
        searchResult.session = session;
        if (session!.groupCode != null) {
          session.group = await groupDao.getByCodeAndUserId(session.groupCode!, userId);
          await groupDao.loadGroupUsers(session.group!);
        } else {
          session.toUser = await userDao.getByUserId(session.toUserId!);
        }
      }
    }
    return result;
  }

  void handleGroupSearchResult(String userId, List<SearchResult> groupSearchResults) {
    for (var searchResult in groupSearchResults) {
      initGroupInfo(searchResult, searchResult!.groupCode!, userId);
    }
  }

  void initGroupInfo(SearchResult searchResult, String groupCode, String userId) async {
    var groupDao = ImGroupDao();
    var groupUserDao = ImGroupUserDao();
    var userDao = ImUserDao();

    var imGroup = await groupDao.getByCodeAndUserId(groupCode, userId);
    var groupUserList = await groupUserDao.getByGroupId(imGroup!.id!);
    imGroup.groupUserList = groupUserList!.toSet();
    for (var gu in groupUserList) {
      var user = await userDao.getByUserId(gu.userId);
      gu.user = user!;
    }
    searchResult.group = imGroup!;
  }

}